import { useUserStore } from "@/stores";
import { useStorage } from "./storage";

let baseHOST = 'https://xcyd.kf.kaifa.cc/app/ycLearn/';
let baseURL = `${baseHOST}api/`;
// #ifdef H5
import system from './h5/system';
// #endif
// #ifdef MP-WEIXIN
import system from './mp-weixin/system'
// #endif;

// #ifdef MP-TOUTIAO
import system from './mp-weixin/system'
// #endif;

if (system.isProd()) {
	baseHOST = `${system.host()}/app/ycLearn/`;
	baseURL = `${system.host()}/app/ycLearn/api/`;
}

const RequestHeaders = (options ?: any) => {
	const { getToken, hasLogin,initUserInfo } = useUserStore();
	initUserInfo()	
	const header : any = {};
	if (hasLogin()) {
		header['Authorization'] =getToken();
	}
	if (options && options.header) {
		for (let x in options.header) {
			header[x] = options.header[x]
		}
	}
	const storeage = useStorage();
	const appid = system.appid();
	if (appid) {
		header['Appid'] = appid;
		const icode = storeage.get('ICODE.' + appid);
		if (icode) {
			header['Icode'] = icode;
		}
		const PUID = storeage.get('PUID.' + appid);
		if (PUID) {
			header['Puid'] = PUID;
		}
	}
	// header['content-type'] = 'application/json';
	header['Accept'] = 'application/json, text/plain, */*';
	return header;
}
export const $http = {
	ResponseCode: {
		SUCCESS: 200,
		NEED_LOGIN: 12000,
		PAY_SUCCESS: 9000,
		PAY_NOTPAY: 11000,
		NEED_PAY: 60000
	},
	baseURL,
	baseHOST,
	get: (url : string, options ?: any) : Promise<any> => {

		const { clearUserInfo } = useUserStore();
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				header: RequestHeaders(options),
				url: baseURL + url,
				success: (res : any) => {
					uni.hideLoading()
					let data = res.data
					if (data.code == 200) {
						resolve(res.data);
					} else {
						if (data.code == -1) {
							clearUserInfo()
						} else {
							uni.showModal({
								title: '温馨提示',
								content: data.msg
							})
						}
						reject(res);
					}
				},
				fail: (err : any) => {
					reject(err);
				},
				complete: () => { }
			})
		})
	},
	post: (url : string, options : any) : Promise<any> => {
		const { clearUserInfo } = useUserStore();
		return new Promise((resolve, reject) => {
			uni.request({
				...options,
				header: RequestHeaders(options),
				url: baseURL + url,
				method: 'POST',
				success: (res : any) => {
					uni.hideLoading()
					let data = res.data
					if (data.code == 200) {
						resolve(res.data);
					} else {
						if (data.code == -1) {
							clearUserInfo()
						}
						uni.showModal({
							title: '温馨提示',
							content: data.msg
						})
						reject(res);
					}
				},
				fail: (err : any) => {
					reject(err);
				},
				complete: () => { }
			})
		})
	},
	upload: (url : string, options : any) : UniApp.UploadTask | Promise<any> => {
		if (!options.formData) {
			options.formData = {}
		}
		options.formData.is_hide = 1
		console.log(options)
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				...options,
				header: RequestHeaders(options),
				url: baseURL + url,
				success: (res : any) => {
					uni.hideLoading();
					let data = res.data
					const isString1 = typeof data === 'string';
					if (isString1) {
						data = JSON.parse(data)
					}
					if (data.code == 200) {
						resolve(data);
					} else {
						uni.showModal({
							title: '温馨提示',
							content: data.msg
						})
						reject(res);
					}
				},
				fail: (err : any) => {
					reject(err);
				},
			})
		})
	}
}